package app

import java.io.File;

 class Utils {

	static public String getFilePath(String dir, String name, String ext) {
		String s = "$dir/$name.$ext";
		println("Utils.getFilePath() : " + s);
		return s;
	}

	static public void cleanDirectory(String path) {
		File dir = new File(path);
		dir.eachFile{
			it.delete();
			println(it.name);
		}
	}

	static public File createFile(String directory,String name, String ext) {

		//First remove the old files if it exist
		cleanDirectory(directory);

		File file = null;
		try {
			file = new File(Utils.getFilePath(directory, name, ext));
			println("Utils.createFile() : " + file.toString());
		}
		catch(Exception e)
		{
			println("Utils.createFile() : " + e.getMessage());
		}
		finally
		{
			return file;
		}
	}

	static public void writeToFile(String directory,String name, String ext, String data) {

		File file = createFile(directory, name, ext);
		//File file = createFile(path);
		println("Utils.writeToFile() : "+file.getAbsolutePath());
		file.append(data);
	}
	
	static public String removeDoubleQuotes(String data) {
		Character doubleQuote = '"'
//		println("removeDoubleQuotes data $data")
		String result = data
		if(result.length()==0)
		{
			return result
		}
		
		try
		{
			if(result.charAt(0)==doubleQuote)
			{
				result=result.substring(1)
			}
			if(result.charAt(result.length()-1)==doubleQuote)
			{
				result=result.substring(0, result.length()-1)
			}
		}
		catch(Exception ex)
		{
			println("remove double quote : $result")
			ex.getMessage()
		}	
	return result			
	}
	
	static public String upperCaseFormat(String data) {
		if(data.length()<2)
		{
			return data.toUpperCase()
		}
		else
		{
			data = data.substring(0,1).toUpperCase() + data.substring(1)
		}
		return data
	}
	
	static public String lowerCaseFormat(String data) {
		if(data.length()<2)
		{
			return data.toLowerCase()
		}
		else
		{
			data = data.substring(0,1).toLowerCase() + data.substring(1)
		}
		return data
	}

	static public String importFormat(String data) {
		
		if(data!=null && !(data.isEmpty()))
		{
//			println("data $data")
			data = removeDoubleQuotes(data)
			data = data.trim()
//			if(!isCaseSensitive)
//			{
//				if(isUpperCase){
					data = upperCaseFormat(data)
//				}
//				if(!isUpperCase){
//					data = lowerCaseFormat(data)
//				}
//			}	
		}
		return data
	}
	
	static public String importFormatLowerCase(String data) {
		
		if(data!=null && !(data.isEmpty()))
		{
			data = removeDoubleQuotes(data)
			data = data.trim()
			data = lowerCaseFormat(data)
		}
		return data
	}
	
	static public String importFormatNoCase(String data) {
		
		if(data!=null && !(data.isEmpty()))
		{
//			println("data $data")
			data = removeDoubleQuotes(data)
			data = data.trim()
//			data = caseFormat(data)
		}
		return data
	}
	
	static public String regex(String s){
		return ".*${s}.*"
	}
 


}
